/*
	ServEngine framework library
	A library to fast develop JEE online applications
	
	Copyright 2008 José Ignacio de Córdoba Álvaro
	
	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at
	
		 http://www.apache.org/licenses/LICENSE-2.0
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
	
	Jose Ignacio de Cordoba Alvaro
	http://ignacio.decordoba.com
*/

package com.servengine;

import java.rmi.RemoteException;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import javax.servlet.ServletContext;

import java.util.Properties;

import java.io.IOException;

import com.servengine.user.*;
import com.servengine.portal.*;

/**
 * Abstract class to implement default cleaning procedures for portal and user removal. Every module developed using ServEngine should extend this class in its main package. ServEngine core engine searches classpath for subclasses and executes methods every time a user or portal is erased in the system.
 */
public abstract class Cleaner
{
	private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(Cleaner.class.getName());

	private InitialContext initialcontext;

	protected UserManagerLocal usermanager;

	protected PortalManagerLocal portalmanager;

	/**
	 * Looks up user and portal managers from URLs at jbossclienturls.properties and invokes initCleaner for custom inicialization. If there is no URL for this class, uses default. If no default is supplied, tries to connect to "jnp://localhost:1099/"+servletcontext.getServletContextName()
	 * @param servletcontext
	 * @throws RemoteException If it is thrown in remote looup of UserManager o PortalManager
	 * @throws NamingException If it is thrown in remote looup of UserManager o PortalManager
	 */
	public Cleaner(javax.servlet.ServletContext servletcontext) throws NamingException
	{
		String url = null;
		try
		{
			Properties properties = new Properties();
			try
			{
				properties.load(getClass().getClassLoader().getResourceAsStream("jbossclienturls.properties"));
			} catch (NullPointerException e)
			{
				log.warn("ClassLoader can't load jbossclienturls.properties");
			}
			if (properties != null)
			{
				url = properties.getProperty(getClass().getName());
				if (url == null)
					url = properties.getProperty("default");
			}
			if (url == null)
			{
				url = "jnp://localhost:1099/" + servletcontext.getServletContextName();
				log.warn("No default url found. Defaults to localhost JBoss url with ServletContextName: " +
						servletcontext.getServletContextName());
				log.warn(url);
			}
		} catch (Throwable e)
		{
			log.error(e.getMessage());
			url = "jnp://localhost:1099/" + servletcontext.getServletContextName();
			log.warn("Using default url:" + url);
		}
		if (log.isDebugEnabled())
			log.debug(getClass().getName() + ":url=" + url);
		//      event.getSession().getServletContext().log(event.getName()+", setting initialcontext="+url) ;
		java.util.Hashtable props = new java.util.Hashtable();
		props.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
		props.put("java.naming.provider.url", url);
		props.put(javax.naming.Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
		initialcontext = new javax.naming.InitialContext(props);
		usermanager = (UserManagerLocal)initialcontext.lookup("UserManager/local");
		portalmanager = (PortalManagerLocal)initialcontext.lookup("PortalManager/local");
		initCleaner(servletcontext, initialcontext);
	}


	/**
	 * Module can initialize any resource needed extending this method.
	 * @param servletcontext
	 * @param initialcontext Can be used to make any Lookups to Managers
	 * @throws IOException
	 * @throws NamingException
	 */
	public abstract void initCleaner(ServletContext servletcontext, InitialContext initialcontext) throws NamingException;

	/**
	 * System will invoke this method every time (but after) a user is removed.
	 * @param portalid
	 * @param userid
	 */
	public abstract void userRemoved(String portalid, String userid);

	/**
	 * System will invoke this method every time (but after) a portal is removed.
	 * @param portalid
	 */
	public abstract void portalRemoved(String portalid);

	/**
	 * @deprecated
	 * Developer can user standard finalize method if wishes to free any resource when garbage collector invokes it.
	 */
	public abstract void finalizeCleaner();

	/**
	 * Every method must return here the space used by supplied portal. System sums space used by every service to set limits for every portal.
	 * @param portalid
	 * @return space used by supplied portal in bytes
	 */
	public abstract long getSpaceused(String portalid);
}